/*
 *	This file is part of Lounge-Cast.
 *
 *  Author:  	Markus Moormann
 *	E-mail:  	mmoorman@uni-osnabrueck.de
 *	Copyright:	University of Osnabrueck (Zentrum Virtuos)
 * 	Website:	www.uni-osnabrueck.de
 *	Date:		16.02.2011
 *
 *	Lounge-Cast is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *	Lounge-Cast is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Lounge-Cast.  If not, see <http://www.gnu.org/licenses/>.
 */
package multimedia.control.command.preview
{
	import com.adobe.cairngorm.commands.ICommand;
	import com.adobe.cairngorm.commands.SequenceCommand;
	import com.adobe.cairngorm.control.CairngormEvent;
	
	import multimedia.control.event.preview.LoadImageEvent;
	import multimedia.model.ModelLocator;
	
	import mx.core.Application;
	import mx.core.FlexGlobals;
	
	import org.osmf.elements.ImageElement;
	import org.osmf.elements.ParallelElement;
	import org.osmf.elements.TextElement;
	import org.osmf.events.MediaElementEvent;
	import org.osmf.layout.LayoutMetadata;
	import org.osmf.layout.LayoutMode;
	import org.osmf.media.MediaType;
	import org.osmf.media.URLResource;
	import org.osmf.traits.DisplayObjectTrait;
	import org.osmf.traits.MediaTraitType;

	public class LoadImageCommand extends SequenceCommand implements ICommand
	{
		
		private var e:LoadImageEvent;

		private var image:ImageElement
		private var image_source:TextElement;
		private var media:ParallelElement;
		
		private var imageLayout:LayoutMetadata
		
		private var model:ModelLocator;
		
		private var width:Number;
		private var height:Number;
		
		override public function execute(event:CairngormEvent):void
		{
			this.e = event as LoadImageEvent;
			this.nextEvent = e.nextEvent;
			
			this.model = ModelLocator.getInstance();

			height = FlexGlobals.topLevelApplication.stage.height * 0.9;
			width = height * (16 / 9);
			
			if (width > FlexGlobals.topLevelApplication.stage.width)
			{
				width = FlexGlobals.topLevelApplication.stage.width * 0.9;
				height = width / (16 / 9);
			}
			
			var imageResource:URLResource = new URLResource(e.image.url);
			imageResource.mediaType = MediaType.IMAGE
			image = new ImageElement(imageResource);
			image.addEventListener(MediaElementEvent.TRAIT_ADD, onTraitAdd);
			
			image_source = new TextElement("Quelle: " + e.image.image_source, false);
			media = new ParallelElement();
			
			imageLayout = new LayoutMetadata();
			image.addMetadata(LayoutMetadata.LAYOUT_NAMESPACE, imageLayout);
			imageLayout.width;
			media.addChild(image);
			media.addChild(image_source);
			
			var parallelLayout:LayoutMetadata = new LayoutMetadata()
			media.addMetadata(LayoutMetadata.LAYOUT_NAMESPACE, parallelLayout);
			parallelLayout.height = height;
			parallelLayout.width = width;
			parallelLayout.layoutMode = LayoutMode.NONE;
			
			model.wrapper.height = height;
			model.wrapper.width = width;
			model.wrapper.x = FlexGlobals.topLevelApplication.stage.width * 0.5 - width * 0.5;
			model.wrapper.y = FlexGlobals.topLevelApplication.stage.height * 0.5 - height * 0.5;
			
			model.wrapper.addMediaElement(media);
			model.wrapper.media = media;
			
			
			executeNextCommand();
		}
		
		private function onTraitAdd(event:MediaElementEvent):void
		{
			if(event.traitType == MediaTraitType.DISPLAY_OBJECT)
			{
				var display:DisplayObjectTrait = image.getTrait(MediaTraitType.DISPLAY_OBJECT) as DisplayObjectTrait;
			}
		}
		
	}
}